# 图片分类
from __future__ import print_function

from my.common.fun import *
from my.common.imgFun import *

from tensorflow.python.framework import dtypes
import glob
import math
import os

from IPython import display
from matplotlib import cm
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn import metrics
import tensorflow as tf
from tensorflow.python.data import Dataset
import pdb

# csv 中特征占的列数
featuresColumnLengthDe = 30000
# csv 总共的数据量
sumRows = 743
# if __name__ == "__main__":
# 把目录中的图片按照标签分类,并且写入到csv中(第一位为 标签,后面的为 特征)
# wAllImgToCsv(r"G:\xampp\htdocs\my\python\imgClassify\男人", 1, r"G:\xampp\htdocs\my\python\imgClassify\manWoman.csv")
# wAllImgToCsv(r"G:\xampp\htdocs\my\python\imgClassify\女人", 0, r"G:\xampp\htdocs\my\python\imgClassify\manWoman.csv")
# pass
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
pd.options.display.max_rows = 10
pd.options.display.float_format = '{:.1f}'.format

# 读取数据集
mnist_dataframe = pd.read_csv(
    r'G:\xampp\htdocs\my\python\imgClassify\manWoman.csv',
    sep=",",
    error_bad_lines=False,
    header=None)
# Use just the first 10,000 records for training/validation.
# mnist_dataframe = mnist_dataframe.head(7)
# 打乱数据集
mnist_dataframe = mnist_dataframe.reindex(np.random.permutation(mnist_dataframe.index))


# mnist_dataframe.describe()
# 提取标签和特征
# 拆分训练集验证集
training_targets, training_examples = parse_labels_and_features(mnist_dataframe[:sumRows - 10], featuresColumnLength=featuresColumnLengthDe)
# training_examples.describe()
validation_targets, validation_examples = parse_labels_and_features(mnist_dataframe[sumRows - 10:], featuresColumnLength=featuresColumnLengthDe)

# validation_examples.describe()


# 显示一个随机样本及其对应的标签。
# rand_example = np.random.choice(training_examples.index)
# _, ax = plt.subplots()
# # print(training_examples.loc[rand_example].values.reshape(28, 28))
# ax.matshow(training_examples.loc[rand_example].values.reshape(100, 100, 3))
# ax.set_title("Label: %i" % training_targets.loc[rand_example])
# ax.grid(False)


#

_ = train_DNN_Classifier_model(
    learning_rate=0.003,
    steps=6000,
    batch_size=50,
    hidden_units=[500, 500, 500, 500],
    training_examples=training_examples,
    training_targets=training_targets,
    validation_examples=validation_examples,
    validation_targets=validation_targets,
    sumRows=sumRows,
    featuresColumnLength=featuresColumnLengthDe,
    periods=15,
    # model_dir=r'G:\xampp\htdocs\my\python\imgClassify\tmpv4f0u8bg'
)
# _.export_saved_model(r'G:\xampp\htdocs\my\python\imgClassify\out')
# tf.keras.experimental.export_saved_model(_.model_dir, r'G:\xampp\htdocs\my\python\imgClassify\out')
printMy('model_dir', _.model_dir)
for x in range(8):
     predictImgUrl(r'G:\xampp\htdocs\my\python\imgClassify\pre\GM' + str(x+1)+'.jpg', _)
for x in range(6):
    predictImgUrl(r'G:\xampp\htdocs\my\python\imgClassify\pre\GW' + str(x+1)+'.jpg', _)
